﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ContractManagermentSystem.Common
{
    public class ConnectionHelper
    {
        /// <summary>
        /// Connection String
        /// </summary>
        private string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; }
        }

        /// <summary>
        /// Excute DataTable
        /// </summary>
        /// <param name="storedProcedureName"></param>
        /// <param name="arrPram"></param>
        /// <returns>DataTable</returns>
        public DataTable ExecuteDataTable(string storedProcedureName, params SqlParameter[] arrayParameters)
        {
            var dataTable = new DataTable();
            dataTable.Locale = CultureInfo.CurrentCulture;
            var connection = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand(storedProcedureName, connection);
            cmd.CommandType = CommandType.StoredProcedure;
            if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
            {
                connection.Open();
            }
            try
            {
                if (arrayParameters != null)
                {
                    foreach (SqlParameter parameter in arrayParameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }
                var adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dataTable);
                return dataTable;
            }
            catch (ArgumentNullException ex)
            {

                throw new ArgumentNullException("Error: " + ex.Message);
            }
            finally
            {
                cmd.Dispose();
                connection.Close();
            }
        }

        /// <summary>
        /// Excute Non Querry
        /// </summary>
        /// <param name="storedProcedureName"></param>
        /// <param name="arrParam"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string storedProcedureName, params SqlParameter[] arrayParameters)
        {
            int rowsAffected;
            var connection = new SqlConnection(ConnectionString);
            var command = new SqlCommand(storedProcedureName, connection);
            command.CommandType = CommandType.StoredProcedure;
            if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
            {
                connection.Open();
            }
            try
            {
                if (arrayParameters != null)
                {
                    foreach (SqlParameter parameter in arrayParameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }
                rowsAffected = command.ExecuteNonQuery();
                return rowsAffected;
            }
            catch (ArgumentNullException ex)
            {

                throw new ArgumentNullException("Error: " + ex.Message);
            }
        }

        public int ExecuteScalar(string storedProcedureName, params SqlParameter[] arrayParameters)
        {
            int rowsAffected;
            var connection = new SqlConnection(ConnectionString);
            var cmd = new SqlCommand(storedProcedureName, connection);
            cmd.CommandType = CommandType.StoredProcedure;
            if (connection.State == ConnectionState.Closed || connection.State == ConnectionState.Broken)
            {
                connection.Open();
            }
            try
            {
                if (arrayParameters != null)
                {
                    foreach (SqlParameter parameter in arrayParameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                }
                rowsAffected = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                return rowsAffected;
            }
            catch (ArgumentNullException ex)
            {
                throw new ArgumentNullException("Error: " + ex.Message);
            }
        }
    }
}
